package com.joelaws.prototype.server.dao;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVStrategy;

import com.joelaws.prototype.client.data.DatingDetails;
import com.joelaws.prototype.client.data.DatingService;
import com.joelaws.prototype.client.data.MessagingService;
import com.joelaws.prototype.client.data.NetworkingDetails;
import com.joelaws.prototype.client.data.NetworkingService;
import com.joelaws.prototype.client.data.Picture;
import com.joelaws.prototype.client.data.User;
import com.joelaws.prototype.client.data.DatingDetails.EyeColor;
import com.joelaws.prototype.client.data.DatingDetails.HairColor;
import com.joelaws.prototype.client.data.NetworkingDetails.Degree;
import com.joelaws.prototype.client.data.NetworkingDetails.Employment;
import com.joelaws.prototype.client.data.NetworkingDetails.Income;
import com.joelaws.prototype.client.data.NetworkingDetails.Religion;

public class DataLoader {

	private static String[][] parseFile(String file) {
		try {
			CSVParser parser = new CSVParser(new InputStreamReader(DataLoader.class.getClassLoader()
					.getResourceAsStream(file)), new CSVStrategy(',', '"', '#'));
			String[][] data = parser.getAllValues();

			if (data == null || data.length <= 1 || data[0].length <= 1) {
				System.out.println("Invalid data file");
				throw new IllegalArgumentException("Invalid data file");
			}
			return data;

		} catch (FileNotFoundException e) {

			throw new IllegalArgumentException("Data file not found", e);
		} catch (IOException e) {

			throw new IllegalArgumentException("Error processing data file", e);
		}
	}

	public static void populateDB(String file) {
		String[][] fileData = parseFile(file);

		for (int i = 1; i < fileData.length; i++) {
			String[] arr = fileData[i];

			User user = new User(arr[0], arr[1], arr[2], arr[3]);
			long userID = i + 1000;
			user.setUserID(userID);

			DatingDetails datingDetails = new DatingDetails(userID, arr[6], EyeColor.valueOf(arr[5]), HairColor
					.valueOf(arr[4]));
			DatingService datingService = new DatingService(userID, datingDetails);

			MessagingService messagingService = new MessagingService(userID);

			DateFormat format = new SimpleDateFormat("mm/dd/yy");
			Date date = null;
			try {
				date = format.parse(arr[8]);
			} catch (ParseException e) {
				e.printStackTrace();
			}

			NetworkingDetails userDetails = new NetworkingDetails(userID, Boolean.parseBoolean(arr[7]), date, Religion
					.valueOf(arr[9]), Degree.valueOf(arr[10]), Employment.valueOf(arr[11]), Income.valueOf(arr[12]));

			String loc = "images/people/" + user.getLastName() + ".png";
			String picName = "com/joelaws/prototype/public/images/people/" + user.getLastName() + ".png";
			if (DataLoader.class.getClassLoader().getResource(picName) == null) {

				picName = "com/joelaws/prototype/public/images/people/" + user.getFirstName() + ".png";
				if (DataLoader.class.getClassLoader().getResource(picName) == null) {
					System.out.println("No Picture: " + user.getFirstName() + " " + user.getLastName());
					loc = "images/space_ghost.jpg";
				} else {

					loc = "images/people/" + user.getFirstName() + ".png";
				}
			}
			Picture picture = new Picture(userID, "pic", loc);

			NetworkingService networkingService = new NetworkingService(userID, picture, userDetails);
			user.setDatingService(datingService);
			user.setNetworkingService(networkingService);
			user.setMessagingService(messagingService);

			DAOFactory.getUserDAO().storeUser(user);
		}
	}
}
